<?php

/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}

require_once('indexApp.php');

class BorrowController extends IndexAppController {

    public function __construct() {
        parent::__construct();
        $this->dealLogic = new DealLogic();
        $this->userLogic = new UserLogic();
    }

    public function actionDefault() {
        T::$tpl->set('user', $this->user);
    }

    //贷款申请
    public function actionSave() {
        T::$tplFile = 'public/empty';
        $res = array();
        $res['code'] = 'error';
        $res['msg'] = '异常';
        $loandata = $_POST;
        unset($loandata["realname"]);
        unset($loandata["username"]);
        if ($loandata["user_id"] == "") {
            $res['code'] = 'error';
            $res['msg'] = '请登录';
            $res['tourl'] = "index.php";
        } else {
            $DealLogic = new DealLogic();
            $count = $DealLogic->getRoleListCount("USER_ID = " . $loandata["user_id"]);
            if ($count > 1) {
                $res['msg'] = '您尚有贷款未还清。请该项目结束，才可申请！';
                $res['code'] = '您尚有贷款未还清。请该项目结束，才可申请';
                $res['tourl'] = 'index.php?ctl=borrow';
            } else {
                $loandata['title'] = '借款' . $loandata['user_id'];
                $loandata['code'] = 'JK' . date('YmdHis', time());
                $result = $DealLogic->addDeal($loandata);
                if ($result) {
                    $res['code'] = 'success';
                    $res['msg'] = '';
                    $res['tourl'] = 'index.php?ctl=borrow&act=step&step=2';
                }
            }
        }
        echo $this->getResources($res);
    }

    public function actionStep() {
        $step = $_REQUEST['step'];
        switch ($step) {
            case 1: T::$tplFile = 'front/borrow/default';
                break;
            case 2: T::$tplFile = 'front/borrow/step2';
                break;
            case 3:T::$tplFile = 'front/borrow/step3';
                 $this->Save2();
                break;
            case 4: T::$tplFile = 'front/borrow/step4';
                break;
            case 5: T::$tplFile = 'front/borrow/step5';
                break;
            default :T::$tplFile = 'front/borrow/default';
        }
        $userItem = $this->userLogic->getUserItem("*", "ID=?", array($this->user['ID']));
        $userItem['userType'] = $this->userLogic->getUserTypeItem("*", "ID=?", array($this->user['USERTYPE_ID']));
        $userItem['info'] = $this->userLogic->getUserInfo("*", "ID=" . $userItem['INFOID']);
        $userItem['info']['RELATIVE'] = $this->string2array($userItem['info']['RELATIVE']);
        $userItem['info']['WORK'] = $this->string2array($userItem['info']['WORK']);
        $userItem['info']['ASSET'] = $this->string2array($userItem['info']['ASSET']);

        //初始变更
        $idcart = $userItem['info']["IDCARD"];
        if (!empty($idcart)) {
            $idcart_c = substr($idcart, 4, strlen($idcart) - 7);
            $userItem['info']["IDCARD"] = str_replace($idcart_c, "**********", $idcart);
        }
        T::$tpl->set("userItem", $userItem);
    }
    public function actionstep2(){
        
    }
    public function Save2() {
        
        $dataArr = array();
        $dataArr['GEO_CODE'] = RequestUtil::getPost('geo_code');
        $dataArr['ADDRESS'] = RequestUtil::getPost('ADDRESS');
        $dataArr['SEX'] = RequestUtil::getPost('SEX');
        $dataArr['IDCARD'] = RequestUtil::getPost('IDCARD');
        $dataArr['EMAIL'] = RequestUtil::getPost('EMAIL');
        $dataArr['QQ'] = RequestUtil::getPost('QQ');
        $dataArr['EDUCATION'] = RequestUtil::getPost('EDUCATION');
        $dataArr['BIRTHDAY'] = RequestUtil::getPost('BIRTHDAY');
        $dataArr['MARRY'] = RequestUtil::getPost('MARRY');
        $dataArr['HASCHILD'] = RequestUtil::getPost('HASCHILD');
        $dataArr['STATE'] = 1;

        $RELATIVE = array();
        $RELATIVE['NAME'] = RequestUtil::getPost('RELATIVE_NAME');
        $RELATIVE['SHIP'] = RequestUtil::getPost('RELATIVE_SHIP');
        $RELATIVE['TEL'] = RequestUtil::getPost('RELATIVE_TEL');
        $dataArr['RELATIVE'] = $this->array2string($RELATIVE, 0);

        $WORK = array();
        $WORK['NAME'] = RequestUtil::getPost('WORK_NAME');
        $WORK['TYPE'] = RequestUtil::getPost('WORK_TYPE');
        $WORK['SIZE'] = RequestUtil::getPost('WORK_SIZE');
        $WORK['STATE'] = RequestUtil::getPost('WORK_STATE');
        $WORK['JOB'] = RequestUtil::getPost('WORK_JOB');
        $WORK['MONEY'] = RequestUtil::getPost('WORK_MONEY');
        $dataArr['WORK'] = $this->array2string($WORK);

        $ASSET = array();
        $ASSET['HOUSE'] = RequestUtil::getPost('HOUSE');
        $ASSET['HOUSE_DEAL'] = RequestUtil::getPost('HOUSE_DEAL');
        $ASSET['CAR'] = RequestUtil::getPost('CAR');
        $ASSET['CAR_DEAL'] = RequestUtil::getPost('CAR_DEAL');
        $ASSET['CAR_ID'] = RequestUtil::getPost('CAR_ID');
        $dataArr['ASSET'] = $this->array2string($ASSET);
        if(!$_POST['btnsubmit']){}
        else {
        $up = $this->userLogic->updateInfoVal($dataArr, "ID=?", array($this->user['INFOID']));
        if ($up) {
            T::$tpl->set("EndJS", "$.jBox.tip('保存成功..', 'error');");
        } else {
            T::$tpl->set("EndJS", "$.jBox.tip('保存失败请重试..', '提示')");
        }
        }
    }

}

//End CLass
?>
